	<?php
	ob_end_clean();
	ob_start();
?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta http-equiv="Content-Script-Type" content="text/javascript" />
		<meta http-equiv="Content-Style-Type" content="text/css" />

	</head>
<body>
<?php
	require_once( '../system/smarty.inc' );
	require_once( '../system/mdao/Class_M_USER.php' );
	require_once( '../system/Class_FILE_CSV.php' );
	require_once( '../system/Class_ERROR.php' );
	require_once( '../system/login.inc.php' );
	require_once( '../system/aoki_m.php' );
	require_once( '../system/Class_PWD.php' );
	
	Class_PWD::valid_auth(AOKI_AUTH_MATRIX,$login['staff_auth']);


	$docsv_import = FALSE;
	
	$csvf = Class_FILE_CSV::get_upload_file();
	
	if ($csvf) {
		// ロケール情報の変更
		$locale = setlocale(LC_ALL, 0);
		setlocale(LC_ALL,'ja_JP.UTF-8');
		
		$csvf = Class_FILE_CSV::fwrite_tmp_utf8($csvf);
		
		if  ($csvf) {
			// CSVファイルのOPEN
			$csv = new Class_FILE_CSV($csvf);
			$csvheads = array('ins_co_name','local_name','ins_shop_cd','ins_shop_name','media_name','delive_name','in_area_name','insert_conums');
			$key1 = 2;//キーです、必ず仕様します
			
			// 読み取り開始
			$csv->fetchRow();
			/*
			if ($this->_skipHeader) {
				$csv->fetchRow();
			}
			*/
			$err = new Class_ERROR("","");
			$dao = new Class_M_USER;
			
			$ngline  = 0;
			$error_times = 0;
			$error_stop = IMPORT_STOP_MAX_ERROR_TIMES;
			$errtext = array();
			$isng = FALSE;
			$chk = $dao->get_checks();
			
			$rowall = array();
			for ($line = 1; $row = $csv->fetchRow(); ++$line) {
				$rowall[$line] = $row;
			}
			$csv->close();
			//print_r($rowall);
			//for ($line = 1; $row = $csv->fetchRow(); ++$line) {
			foreach ($rowall as $line => $row) {
				$isng = FALSE;
				//CSV内部重複不可チェック
				foreach ($rowall as $kk=>$vv) {
					if  ($line != $kk) {
						if ($row[$key1] == $vv[$key1]){
							$col = $key1 + 1;
							$isng = TRUE;
							$ngMsg_tmp[$line][] = "{$line}行と{$kk}行の{$col}列目の値重複不可。";
							$error_times++;
							if ($error_times > $error_stop) {
								break;
							}
						}
					}
				} 
				
				foreach ( $row as $k=>$v ) {
					$msg =  $err->check( $v, $chk[ $csvheads[ $k ] ] ); 
					if (!empty($msg)) {
						$isng = TRUE;
						$col  = $k+1;
						$ngMsg_tmp[$line][] = "{$line}行{$col}列目：{$msg}。";
						$error_times++;					
						if ($error_times > $error_stop) {
							break;
						}
					}
				}
				
				if ( $isng ) {
					++$ngline;
					$errtext[$ngline] = $row;
					if ($error_times > $error_stop) {
						break;
					}
				}
			}
			
			if ($ngline > 0) {
				$errHtml = Class_FILE_CSV::format_msg($ngMsg_tmp);
				if ($error_times > $error_stop) {
					Class_FILE_CSV::write_msg_error( $errHtml ,$error_stop );	
				
				} else {
					Class_FILE_CSV::write_msg_error( $errHtml );	
				}
			} else {
				//DBへ更新
				
				try {
					$dao->begin_trans();
					$i = 0;
					$percent = "";
					$percentold = "";
					foreach ( $rowall as $k=>$v ) {
						$i++;
						$percent = intval($i/$line * 100)."%";
						if  ( $percent != $percentold ) {
							Class_FILE_CSV::write_msg_doing( $percent ) ;
						}
						$percentold = $percent;
						$values    = array();
						
						foreach ( $v as $kk => $vv ) {
							$values[$csvheads[$kk]] = $vv;
						}
						$keyvalues =  $values[$csvheads[$key1]];
						$where = sprintf(" {$csvheads[$key1]} = '%s'", $keyvalues );
						if ( $dao->exits( $where ) ) {
							if (!($dao->editbycol($keyvalues ,$values ,$csvheads[$key1] ))) {
								throw new Exception("mmatrix_import db更新");
							}
							
						} else {
							if ( !( $dao->add($values ) ) ) {
								throw new Exception("mmatrix_import db更新");
							}
						}
					}
					Class_FILE_CSV::write_msg_succed($line);
					$dao->commit_trans();
					
					
				    //echo '<script language="javascript">parent.location.href = "mmatrix_import.php?pro=sucess"</script>';
				} catch (Exception $e) {
					$dao->rollback_trans();
					Class_FILE_CSV::write_msg_error( "DB Exceptionがありました。" );
				}
			} 
		}
		
		// ロケール情報の復元
		setlocale(LC_ALL, $locale);
	
	}
?>
</body>
	</himl>
	